#include <string.h>
#include <stdio.h>
#include <conio.h>

#include "FileFunc.h"		// added file to do file handling

typedef struct ROM{
 dwrd ptr[0x100]; // the pointers that need patching
 dwrd ofs;				// offset of data
 byte adr;				// amount of alter offsets
 byte cof;				// current offset into
 byte dat;    		// data read variable
 char mde;				// ROM mode
} ROMDATA;

 word aw,xw,yw;

 byte ab,xb,yb;

 ROMDATA ips;

 char *ep;
 char fil1[260];
 char fil2[260];

 FILE *f1,*f2;

dwrd Lo2ROM(dwrd);

char main(argc, argv)
 int argc;
 char *argv[];
{
	clrscr();
	if ( argc > 2 )
	{ //
		strcpy(fil1, argv[1]); if ( !(f1 = OpenFile(fil1,"r+b")) ) exit(0);
		strcpy(fil2, argv[2]); if ( !(f2 = OpenFile(fil2,"r+b")) ) exit(0);
	}
	else
	{ //
		clrscr();
		printf("Usage: %s\nROM.file FakeIPS.file",argv[0]);
		exit(0);
	}// END IF

	// Confirm files to insert
	clrscr();
	ab = 0;
	printf("Inserting\n %s\ninto\n %s\n\nIs this correct?\n[Y] - Yes\n[N] - No",
	       fil2,
				 fil1
	      );
  //
	while( toupper(ab) != 'Y' && toupper(ab) != 'N') ab = getch();
	 if ( toupper(ab) == 'N' ){
	   fcloseall( );
		 exit( 0 );
	 }

	// Clear varibles
	ips.cof = 0;
	ips.ofs = 0;
	ips.mde = 0;

	// Load up header
	fseek(f2,0,SEEK_SET);
	ips.adr = fgetc(f2);						// get the amount of offsets MAX 254, FF = lo-rom
	// Test for LoRom
	if (ips.adr == 0xFF){
		ips.mde = 1;
		ips.adr = fgetc(f2);   				// get the amount of offsets MAX 254, FF = lo-rom
	}// END IF

	// read in all the offsets
	fread(&ips.ptr,4,ips.adr,f2);

	// all offsets are - $200 header
	// Test for Hi - Lo ROM
	if (ips.mde){ // Lo-ROM
		// Start patching ROM
		while( ips.cof < ips.adr ){
			// subtract the hi-rom bank value
			ips.ptr[ips.cof+0] = Lo2ROM(ips.ptr[ips.cof+0]);
			ips.ptr[ips.cof+1] = Lo2ROM(ips.ptr[ips.cof+1]);

			// goto ptr + $200 header for ROM
			fseek(f1,ips.ptr[ips.cof] + 0x0200,SEEK_SET);
			// goto ptr for IPS file
			fseek(f2,ips.ptr[ips.cof],SEEK_SET);

			// Start reading data
			ips.dat = fgetc(f2);
			ips.ofs = ips.ptr[ips.cof] + 0x0200;
      //
			while( ips.ofs < ( ips.ptr[ips.cof+1] + 0x0200) ){
				fputc(ips.dat,f1);
				ips.dat = fgetc(f2);
				ips.ofs++;
			}// WEND
			ips.cof+=2;
		}// WEND


	}else{		// Hi-ROM
		// Start patching ROM
		while( ips.cof < ips.adr ){
			// subtract the hi-rom bank value
			ips.ptr[ips.cof] -= 0xC00000; ips.ptr[ips.cof+1] -= 0xC00000;
			// goto ptr + $200 header for ROM
			fseek(f1,ips.ptr[ips.cof] + 0x0200,SEEK_SET);
			// goto ptr for IPS file
			fseek(f2,ips.ptr[ips.cof],SEEK_SET);

			// Start reading data
			ips.dat = fgetc(f2); ips.ofs = ips.ptr[ips.cof] + 0x0200;
			while( ips.ofs < ( ips.ptr[ips.cof+1] + 0x0200) ){
				fputc(ips.dat,f1);
				ips.dat = fgetc(f2);
				ips.ofs++;
			}// WEND
			ips.cof+=2;
		}// WEND
	}// END IF
	fcloseall();
	printf("\nData inserted!");
	return 0;
}// END OF MAIN



/***************************
; Convert Value from LoRom *
***************************/
dwrd Lo2ROM(dwrd sofs)
{
 if (sofs > 0x800000)	sofs -= 0x800000;

 return ((sofs & 0xFF0000)>>1) + ((sofs & 0xFFFF) - 0x8000);
}